<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="标题" prop="title">
        <el-input
            style="width: 500px"
            placeholder="请输入标题"
            maxlength="300"
            v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="上传封面" prop="coverImage">
        <el-button type="primary" @click="uploads.show=true" size="mini">选择文件</el-button>
      </el-form-item>
      <el-form-item  prop="coverImage">
        <el-image v-if="form.coverImage"
            style="width: 100px; height: 100px"
            :src="form.coverImage"
            fit="fill">
        </el-image>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <Tinymce v-model="form.content"></Tinymce>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary" @click="handlerSubmit"  v-has-permi="['admin:news:save']">保存</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
        :visible.sync="uploads.show"
        title="上传文件"
        :append-to-body="true"
        destroy-on-close
        :close-on-click-modal="false"
        width="1000px">
      <UploadFile
          v-if="uploads.show"
          :multiple="uploads.multiple"
          @getFile="getFile"
          ref="uploads"
      />
    </el-dialog>
  </div>
</template>
<script>
import {detail, save} from "@/api/news";
import {getForm} from "@/utils/utils";
import UploadFile from "@/components/File/UploadFile.vue";
import Tinymce from "@/components/Tinymce/index.vue";
import {getToken} from "@/utils/auth";

export default {
  components: {Tinymce, UploadFile},
  props:{
    id:{
      default:null
    }
  },
  data(){
    return{
      myHeaders: { "X-Token": getToken() },
      form:{
        id:null,
        title:'',
        coverImage:'',
        content:''
      },
      rules:{
        title: [{ required: true, message: '请输入标题', trigger: ['blur'] }],
        coverImage: [{ required: true, message: '请上传封面', trigger: ['blur'] }],
        content: [{ required: true, message: '请上传封面', trigger: ['blur'] }],
      },
      url:'',
      roles:[],
      saveLoading:false,
      formLoading:false,
      uploads:{
        show:false,
        multiple:true,
      }
    }
  },
  mounted() {
    if(this.id){
      this.getDetail();
    }
  },
  methods:{
    getFile(fileList){
      this.form.coverImage = fileList[0]
      this.uploads.show = false
    },
    getDetail(){
      this.formLoading=true
      detail(this.id).then(res=>{
        getForm(res,this.form);
      }).finally(()=>{
        this.formLoading=false;
      })
    },
    handlerSubmit(){
      if(this.saveLoading||this.formLoading){
        return;
      }
      this.$refs.form.validate(valid=>{
        if(!valid)return;
        this.saveLoading=true;
        save(this.form).then(()=>{
          this.$message.success('保存成功');
          this.close();
        }).finally(()=>{
          this.saveLoading=false
        })
      })
    },
    close(){
      this.$emit('hideEditDialog')
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep{
  .el-icon-picture-outline::before{
    width: 100%;
    height: 100%;
  }
}
.edit-container{
  height: 600px;
}
</style>
